const app = require("express").Router();
const { Videos } = require("../model/index");
const Sequelize = require("sequelize");
const Op = Sequelize.Op;

app.get("/videolist", (req, res) => {
  const { title, page,id } = req.query;
  let offset = (page - 1) * 10;
  if (!page) {
    res.send({ code: -1, msg: "页码未传" });
    return;
  }
  let where = {
    title: {
      // 模糊查询
      [Op.like]: "%" + title + "%",
    },
  };
  if(id)where.id=id
  Videos.findAndCountAll({
    limit: 100,
    offset,
    where,
  })
    .then((ret) => {
      if (ret) {
        res.send({ code: 0,data:ret });
      } else {
        res.send({ code: -1 });
      }
    })
    .catch((err) => {
      res.send({ code: -1, msg: err });
    });
});

app.post("/addvideo", (req, res) => {
  Videos.create(req.body)
    .then((ret) => {
      if (ret) {
        res.send({ code: 0 });
      } else {
        res.send({ code: -1 });
      }
    })
    .catch((err) => {
      res.send({ code: -1, msg: err });
    });
});

module.exports = app;
